//
//  UIScreen+DDAdd.m
//  AFNetworking
//
//  Created by 许龙 on 2020/3/25.
//

#import "UIScreen+DDAdd.h"
#import "UIApplication+DDAdd.h"
#import "UIDevice+DDAdd.h"

@implementation UIScreen (DDAdd)

+ (CGFloat)screenScale {
    static CGFloat screenScale = 0.0;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if ([NSThread isMainThread]) {
            screenScale = [UIScreen mainScreen].scale;
        } else {
            dispatch_async(dispatch_get_main_queue(), ^{
                screenScale = [UIScreen mainScreen].scale;
            });
        }
    });
    return screenScale;
}

- (CGRect)currentBounds {
    return [self boundsForOrientation:[[UIApplication sharedExtensionApplication] statusBarOrientation]];
}

- (CGRect)boundsForOrientation:(UIInterfaceOrientation)orientation {
    CGRect bounds = [self bounds];
    if ((NSFoundationVersionNumber <= NSFoundationVersionNumber_iOS_7_1) && UIInterfaceOrientationIsLandscape(orientation)) {
        CGFloat width = bounds.size.width;
        bounds.size.width = bounds.size.height;
        bounds.size.height = width;
    }
    return bounds;
}

- (CGFloat)width {
    return [self currentBounds].size.width;
}

- (CGFloat)height {
    return [self currentBounds].size.height;
}

- (CGFloat)statusBarHeight {
    return UIInterfaceOrientationIsLandscape([[UIApplication sharedExtensionApplication] statusBarOrientation]) ? 0 : ([[UIDevice currentDevice] isIPhoneXSeries] ? 44 : 20);
}

- (CGFloat)navigationBarHeight {
    return [[UIDevice currentDevice] isIPhoneXSeries] ? 44 : (UIInterfaceOrientationIsLandscape([[UIApplication sharedExtensionApplication] statusBarOrientation]) ? 32 : 44);
}

- (CGFloat)tabBarHeight {
    return [[UIDevice currentDevice] isIPhoneXSeries] ? (UIInterfaceOrientationIsLandscape([[UIApplication sharedExtensionApplication] statusBarOrientation]) ? 32 : 49) : 49;
}

- (CGFloat)bottomHeight {
    return [[UIDevice currentDevice] isIPhoneXSeries] ? (UIInterfaceOrientationIsLandscape([[UIApplication sharedExtensionApplication] statusBarOrientation]) ? 21 : 34) : 0;
}

- (CGFloat)topHeight {
    return [self statusBarHeight] + [self navigationBarHeight];
}

- (CGFloat)hotSpotHeight {
    return (([UIApplication sharedExtensionApplication].statusBarFrame.size.height - [self statusBarHeight]));
}

@end
